<?php
namespace application\cms\controller\admin;


class Article{

    //文章首页
    function index(){   
        //查询文章列表

        $sql="select a.*,s.sort_name from ".tablename('rj_cms_article') 
                        ." a left join " .tablename('rj_cms_article_sort')
                        ." s on a.sort_id=s.sort_id ";
        
        if(isset($_GET['sort_id']) && $_GET['sort_id']>0 ){
            $sort_id = intval($_GET['sort_id']);
            $sql .= ' where a.sort_id = ' . $sort_id;
        }
        if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' ){
            $keyword = trim($_REQUEST['keyword']);
            $sql .= " where a.title like '%$keyword%' ";
        }
        
        $sql.=		" order by a.update_time desc,a.id desc ";

        //$page=new \library\deep\Page(20);
        $page= \library\deep\Page::new_instance();

        $sql=$page->getPageSQL( $sql , 20 );

        $rs=$GLOBALS['db']->query($sql);
        
        require view('admin/article/article.tpl.php');
    } 

    //添加文章页面
    function article_add(){
        //查询分类
        $sql="select * from ".tablename('rj_cms_article_sort')." where status=1 order by parent_path,list_order ";
        $rs_sort=$GLOBALS['db']->query($sql)->fetchall();

        require view('admin/article/article_add.tpl.php');
    }

    //修改文章页面
    function article_edit(){
        //查询分类
        $sql="select * from ".tablename('rj_cms_article_sort')." where status=1 order by parent_path,list_order ";
        $rs_sort=$GLOBALS['db']->query($sql)->fetchall();

        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="select * from ".tablename('rj_cms_article')." where id=".$id;
            $ret=$GLOBALS['db']->query($sql)->fetch();
        }else{
            message('未找到该文章,请刷新页面',url([ _MODULE_NAME_ ,'admin/article','index']));
        }
        require view('admin/article/article_edit.tpl.php');
    }

    //保存
    function article_save(){
        if(!isset($_POST['title']) && $_POST['title']==''){
            message('文章名称不能为空',url([ _MODULE_NAME_ ,'admin/article','index']));
        }

        $data = [
            'sort_id'=>$_POST['sort_id'],
            'title'=>$_POST['title'],
            'flag'=> isset($_POST['flag']) ? implode(',',$_POST['flag'] ) : '',
            'tip'=>$_POST['tip'],
            'keyword'=> $_POST['keyword'],
            'article_img'=> isset($_POST['article_img'])?$_POST['article_img']:'',
            'content'=>$_POST['content'],
            'hits'=>$_POST['hits'],
            'create_time'=>strtotime( $_POST["create_time"] ),
            'update_time'=>strtotime( $_POST["update_time"] ),
            'tpl_name' => $_POST["tpl_name"] ,
            'status'=>$_POST['status'],
        ];

        if( isset($_POST['id']) && $_POST['id']!=''){
            //修改
            //条件
            $where['id']= intval($_POST['id']);

            $ok = pdo_update('rj_cms_article',$data,$where);
            if($ok){
                message('修改成功',url([ _MODULE_NAME_ ,'admin/article','index']));
            }else{
                message('修改失败',url([ _MODULE_NAME_ ,'admin/article','index']));
            }
        }else{
            //添加
            //$data['create_time']= strtotime( $_POST["create_time"] );

            $ok = pdo_insert('rj_cms_article',$data);
            if($ok){
                message('添加成功',url([ _MODULE_NAME_ ,'admin/article','article_add']));
            }else{
                message('添加失败',url([ _MODULE_NAME_ ,'admin/article','article_add']));
            }
        }
    }
    
    //删除文章
    function article_delete(){
        
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="delete from ".tablename('rj_cms_article')." where id=".$id;
            $ok=$GLOBALS['db']->exec($sql);
            if($ok){
                message('删除成功',url([ _MODULE_NAME_ ,'admin/article','index']));
            }else{
                message('删除失败',url([ _MODULE_NAME_ ,'admin/article','index']));
            }
        }else{
            message('未找到该文章,请刷新页面',url([ _MODULE_NAME_ ,'admin/article','index']));
        }
    }

}
